<?php
/**
 * @class pages FAQ module
 * @author Bethrezen <admin@devgroup.ru>
 * @desc Handles all FAQ related stuff.
 */
class faq
{
	var $core;
	
	var $tableName = 't_faq';
	
	var $action='';
	
	var $cur_page = 1;
	var $total_pages = 1;
	var $limit = 10;
	
	function faq(&$_core)
	{
		
		
		$this->core = $_core;
		
		$this->core->adminLinks['faq'] = 'FAQ';
		
		if (!file_exists(DB_DIR . $this->core->config->dbName . "/" . $this->tableName . ".txt"))
		{
		
			$SQL = "CREATE TABLE " . $this->tableName . " ( id inc , question str , answer str , show str , from str )";
			
			$this->core->db->executeQuery($SQL);
			
			$this->core->db->executeQuery("INSERT INTO " . $this->tableName . " ( question , answer , show, from ) VALUES ( 'Как пользоваться FCore' , 'Скоро напишу' , 'yes' , 'Vasya' )");
		}
		
		$action = 'main';
		if (isset($_GET['actionFAQ']))
			$action = $_GET['actionFAQ'];
		if (isset($_POST['actionFAQ']))
			$action = $_POST['actionFAQ'];
				
		$this->action = $action;
		
	}
	
	/**
	 * Returns ask URL
	 */
	function makeAskUrl()
	{
		if ($this->core->config->useModRewrite)
		{
			return $this->core->getDirectoryName() . "/faq/ask.html";
		}
		else
		{
			
			return basename($_SERVER['SCRIPT_NAME']) . "?action=faq&actionFAQ=ask";
		}
	}
	
	
	/**
	 * Ask :-)
	 */
	function ask()
	{
		if (isset($_POST['question']))
		{
			mail ($this->core->adminEmail, 'Новый вопрос в FAQ','Новый вопрос в FAQ: ' . $_POST['question']);
			$question = addslashes($_POST['question']);
			$from = addslashes($_POST['from']);
			$this->core->db->executeQuery("INSERT INTO " . $this->tableName . " ( question , answer, show, from ) VALUES ( '$question' , '' , 'no', '$from' )");
			echo "Ваш вопрос был принят.";
		}
	}
	
	/**
	 * Returns the FAQ page.
	 *
	 */
	function getFAQ($_limit)
	{
		$this->limit = $_limit;
		$record = array();
		$up = 0;
		$this->cur_page = 1;
		if (isset($_GET['p'])==true)
		{
			$this->cur_page = intval($_GET['p']);
			$up = ($this->cur_page-1)*$this->limit;
		
		}
		
		$records = array();
		$q4pages = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName . " WHERE show='yes' ORDER BY question LIMIT $up, " . $this->limit);
		if ($q4pages->getRowCount() != 0)
		{
			while ($q4pages->next())
				$records[] = $q4pages->getCurrentValuesAsHash();
			
			
			
		}
		
		return ($records);
	}
	
	function getLastFAQ($num)
	{
		$record = array();
		
		$q4pages = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName . " ORDER BY id DESC LIMIT $num");
		if ($q4pages->getRowCount() != 0)
		{
			while ($q4pages->next())
			
				$record[] = $q4pages->getCurrentValuesAsHash();
			
		}
		
		return ($record);
	}
	
	function makeListUrl($p)
	{
		if ($this->core->config->useModRewrite)
		{
		
			return $this->core->getDirectoryName() . "/faq/" . $p . ".html";
		}
		else
		{
		
			return basename($_SERVER['SCRIPT_NAME']) . "?action=faq&p=".$p;
		}
	}
	
	//! Paginator array
	function getPaginator()
	{
		$paginator = array();
		$q4paginator = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName);
		$this->total_pages = ceil($q4paginator->getRowCount() / $this->limit);
		
		$paginator = range(1,$this->total_pages);
		
		return $paginator;
	}
	
	/**
	 * Mian admin action
	 *
	 */
	function admin()
	{
		
		$actFunc = "admin" . $this->action;
		$this->$actFunc();
	}
	
	function adminMain()
	{	
		
		$q4pages = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName . " ORDER BY question ASC  ");
		if ($q4pages->getRowCount() != 0)
		{
			?>
			<h2>FAQ</h2>
			<table cellpadding="0" cellspacing="2" border="0"><TR><td>Вопрос</td><td>Отображается</td><td>Действия</td></TR>
			
			<?php
		
			while ($q4pages->next())
			{
				$record = $q4pages->getCurrentValuesAsHash();
				?>
				<tr><TD><?php if ($record['answer']=='') echo "<em>"; ?><?=$record['question'];?><?php if ($record['answer']=='') echo "</em>"; ?></TD>
				<TD><?=$record['show'];?></TD><TD><a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=faq&actionFAQ=Edit&id=<?=$record['id']?>">Редактировать</a> <a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=faq&actionFAQ=Remove&id=<?=$record['id']?>">Удалить</a></TD></tr>
				<?php
			}	
				
			?>
			</table>
			<a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=faq&actionFAQ=Add">Добавить</a>
			<?php
		}
		
	}
	
	function adminRemove()
	{
		if (isset($_GET['id']))
		{
			$SQL = "DELETE FROM " . $this->tableName . " WHERE  id   = '" . intval($_GET['id']) . "' ";
			
				$this->core->db->executeQuery($SQL);
		}
		
		$this->adminMain();
	}
	
	function adminEdit()
	{
		if (isset($_GET['id']))
		{
			$q4edit = $this->core->db->executeQuery("SELECT * FROM " . $this->tableName . " WHERE id = '" . intval($_GET['id']) . "'");
			if ($q4edit->getRowCount() != 0)
			{
				$q4edit->next();
				$record = $q4edit->getCurrentValuesAsHash();
				?>
				<h2>Редактировать вопрос</h2>
		<form action="<?=basename($_SERVER['SCRIPT_FILENAME']);?>" method="POST">
		<input type="hidden" name="actAdmin" value="faq">
		<input type="hidden" name="actionFAQ" value="Edit">
		<input type="hidden" name="id" value="<?=$record['id'];?>">
		<table cellspacing="2" cellpadding="0" border="0">
		<TR><TD>Вопрос:</TD><TD><input type="text" name="question" value="<?=$record['question'];?>"></TD></TR>
		<TR><TD>Ответ:</TD><TD><textarea name="answer"><?=$record['answer'];?></textarea></TD></TR>
		<TR><TD>От кого:</TD><TD><input type="text" name="from" value="<?=$record['from'];?>"></TD></TR>
		<TR><TD>Отображать:</TD><TD><input type="checkbox" name="show" value="yes" <?php if ($record['show']=='yes') echo "checked=\"checked\"";?>></TD></TR>
		<TR><TD>Редактирование:</TD><TD><input type="submit" value="Сохранить"></TD></TR>
		</table>
		</form><br />
		<a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=faq">Назад в FAQ</a>
				<?php
			
			}
			
		}
		if (isset($_POST['id']))
		{
			$question = addslashes($_POST['question']);
			$answer = addslashes($_POST['answer']);
			$from = addslashes($_POST['from']);
			$show = 'no';
			if (isset($_POST['show']))
				if ($_POST['show'] == 'yes')
					$show = 'yes';
					
			$SQL = "UPDATE " . $this->tableName . " SET question='$question' , answer='$answer', show='$show', from='$from' WHERE id='" . intval($_POST['id']) . "'";
			$this->core->db->executeQuery($SQL);
			
			$this->adminMain();
		}
	}
	
	function adminAdd()
	{
		if (!isset($_POST['actAdmin']))
		{
		?>
		<h2>Добавить FAQ</h2>
		<form action="<?=basename($_SERVER['SCRIPT_FILENAME']);?>" method="POST">
		<input type="hidden" name="actAdmin" value="faq">
		<input type="hidden" name="actionFAQ" value="Add">
		<table cellspacing="2" cellpadding="0" border="0">
		<TR><TD>Вопрос:</TD><TD><input type="text" name="question" value=""></TD></TR>
		<TR><TD>Ответ:</TD><TD><textarea name="answer"></textarea></TD></TR>
		<TR><TD>От кого:</TD><TD><input type="text" name="from" value=""></TD></TR>
		<TR><TD>Отображать:</TD><TD><input type="checkbox" name="show" value="yes" checked="checked"></TD></TR>
		<TR><TD>Добавить:</TD><TD><input type="submit" value="Сохранить"></TD></TR>
		</table>
		</form><br />
		<a href="<?=basename($_SERVER['SCRIPT_FILENAME']);?>?actAdmin=faq">Назад в страницы</a>
		<?php
		}
		else
		{
			if (isset($_POST['question']))
			{
				$question = addslashes($_POST['question']);
				$answer = addslashes($_POST['answer']);
				$from = addslashes($_POST['from']);
				$show = 'no';
				if (isset($_POST['show']))
					if ($_POST['show'] == 'yes')
						$show = 'yes';
						
				$SQL = "INSERT INTO " . $this->tableName . " ( question , answer, show , from ) VALUES ( '$question' , '$answer' , '$show' , '$from' )";
				$this->core->db->executeQuery($SQL);
				
				$this->adminMain();
			}
			else
			{
				echo ("Ошибка.");
			}
			
		}
	}
}
